package com.rd.student.jiaji.class3.sunguowei.day27;

import java.io.*;

public class TestInputStream {
    public static void main(String[] args) throws Exception {
        testRead1();

    }

    public static void testRead() throws Exception {
        //        File file=new File("stream.txt");
        InputStream is = new FileInputStream("stream.txt");
        //read()
        //一个字节一个字节读数据
        while (true) {
            //反复调用此方法  直到返回-1为止
            int len = is.read();
            if (len == -1) break;

            byte[] arr = new byte[1];
            arr[0] = (byte) len;
            System.out.print(new String(arr));
        }
        is.close();
    }

    public static void testRead1() throws Exception {
        File file = new File("stream.txt");
        InputStream is = new FileInputStream("stream.txt");
        //read()
        //一个字节一个字节读数据
        int cnt = 0;
        while (true) {
            byte[] arr = new byte[28];
            int len = is.read(arr);
            if (len == -1) {
                System.out.println();
                System.out.println(cnt);
                System.out.println("读取完成");
                break;
            }
            String content = new String(arr);
            System.out.print(len + "--" + content);
            cnt++;
        }
        is.close();
    }

    public static void testClose() {
        InputStream is = null;
        try {
            is = new FileInputStream("stream.txt");
            int cnt = 0;
            while (true) {
                byte[] arr = new byte[28];
                int len = is.read(arr);
                if (len == -1) {
                    System.out.println();
                    System.out.println(cnt);
                    System.out.println("读取完成");
                    break;
                }
                String content = new String(arr);
                System.out.print(len + "--" + content);
                cnt++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void testAutoClose() {
        // 程序里打开的IO流不属于内存资源  垃圾回收机制无法回收
        // 所以需要手动关闭
        // 因为手动关闭流资源非常繁琐  又容易忘记
        // jdk7以后提供了一种自动关闭 IO流资源的机制
        try (InputStream is = new FileInputStream("stream.txt")) {
            int cnt = 0;
            while (true) {
                byte[] arr = new byte[28];
                int len = is.read(arr);
                if (len == -1) {
                    System.out.println();
                    System.out.println(cnt);
                    System.out.println("读取完成");
                    break;
                }
                String content = new String(arr);
                System.out.print(len + "--" + content);
                cnt++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void testRead2() throws Exception {
        File file = new File("stream.txt");
        InputStream is = new FileInputStream("stream.txt");
        //read()
        //一个字节一个字节读数据
        int cnt = 0;
        while (true) {
            byte[] arr = new byte[28];
            int len = is.read(arr, 0, arr.length);
            if (len == -1) {
                System.out.println();
                System.out.println(cnt);
                System.out.println("读取完成");
                break;
            }
            String content = new String(arr, 0, len);
            System.out.print(len + "--" + content);
            cnt++;
        }
        is.close();

    }
}
